package com.coke.im.client;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.UnsupportedEncodingException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.coke.im.client.listener.MessageListener;
import com.coke.im.client.stream.Message;
import com.coke.im.client.type.ExecStatus;
import com.coke.im.client.type.MessageHeadConstant;
import com.coke.im.client.type.MessageStatus;

public class ClientApplication extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6598729813148648250L;
	/**
	 * 
	 */
	private JPanel contentPane;
	private JTextField host;
	private JTextField port;
	private JTextField sendUser;
	private JTextField revUser;
	
	private IMClient mIMClient;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClientApplication frame = new ClientApplication();
					
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ClientApplication() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 491, 376);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 43, 337, 139);
		contentPane.add(scrollPane);
		
		JTextArea showMessage = new JTextArea();
		scrollPane.setViewportView(showMessage);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(10, 200, 337, 79);
		contentPane.add(scrollPane_1);
		
		JTextArea sendMessage = new JTextArea();
		scrollPane_1.setViewportView(sendMessage);
		
		JButton subMessage = new JButton("发送消息");
		subMessage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				try {
					mIMClient.sendMessage(revUser.getText(),sendMessage.getText());
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		});
		subMessage.setBounds(238, 289, 109, 23);
		contentPane.add(subMessage);
		
		JLabel label = new JLabel("服务器：");
		label.setBounds(10, 10, 54, 15);
		contentPane.add(label);
		
		host = new JTextField();
		host.setText("127.0.0.1");
		host.setBounds(62, 7, 101, 21);
		contentPane.add(host);
		host.setColumns(10);
		
		JLabel label_1 = new JLabel("接口：");
		label_1.setBounds(167, 10, 54, 15);
		contentPane.add(label_1);
		
		port = new JTextField();
		port.setText("4646");
		port.setBounds(205, 7, 66, 21);
		contentPane.add(port);
		port.setColumns(10);
		
		JLabel statusLabel = new JLabel("");
		statusLabel.setBounds(357, 264, 108, 15);
		contentPane.add(statusLabel);
		
		JButton login = new JButton("登录");
		login.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				
				mIMClient = new IMClient(host.getText(),Integer.parseInt(port.getText()));
				try {
					mIMClient.setUser(sendUser.getText(), sendUser.getText());
					mIMClient.setMessageListener(new MessageListener() {
						
						@Override
						public void onReceiveMessage(Message msg) {
							try {
								showMessage.append(msg.getHead(MessageHeadConstant.FROM)+":"+new String(msg.getBody(),"UTF-8")+"\r\n");
							} catch (UnsupportedEncodingException e) {
								e.printStackTrace();
							};
							
						}
						
						@Override
						public void onMessageSatus(String msgId, MessageStatus status) {
						
							statusLabel.setText(status.name());
						}
						
						@Override
						public void onExecStatus(ExecStatus status) {
							
							statusLabel.setText(status.name());
							
						}
					});
					mIMClient.start();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				
			}
		});
		login.setBounds(371, 168, 66, 23);
		contentPane.add(login);
		
		JLabel label_2 = new JLabel("发送方：");
		label_2.setBounds(354, 45, 54, 15);
		contentPane.add(label_2);
		
		sendUser = new JTextField();
		sendUser.setText("333333");
		sendUser.setBounds(354, 70, 83, 21);
		contentPane.add(sendUser);
		sendUser.setColumns(10);
		
		JLabel label_3 = new JLabel("接收方：");
		label_3.setBounds(354, 101, 54, 15);
		contentPane.add(label_3);
		
		revUser = new JTextField();
		revUser.setText("333333");
		revUser.setBounds(354, 126, 83, 21);
		contentPane.add(revUser);
		revUser.setColumns(10);
		
		JLabel label_4 = new JLabel("状态");
		label_4.setBounds(357, 232, 54, 15);
		contentPane.add(label_4);
		
		JButton btnNewButton = new JButton("断开");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				if(mIMClient!=null) {
					mIMClient.stop();
				}
				
			}
		});
		btnNewButton.setBounds(370, 201, 66, 23);
		contentPane.add(btnNewButton);
		
		System.out.println();
	}
}
